# _*_ coding utf-8 _*_
# FILENAME：.py
# DESCRIPTION: 
# AUTH: 
# DATE: 2022/6/11:32 下午

import sys
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QTextEdit, QHBoxLayout, QVBoxLayout, \
    QLineEdit, QLabel, QTextBrowser
import sqlite3


class MainWidget(QMainWindow):
    def __init__(self):
        super(MainWidget, self).__init__()
        self.ui_init()
        self.connect_init()
        self.sql_init()

    def ui_init(self):
        self.setWindowTitle('信息查询')
        self.resize(600, 700)

        self.addwidget = AddWidget()
        self.deletewidget = DeleteWidget()
        self.updatewidget = UpdateWidget()

        self.show_view = QTextBrowser()
        self.find_text = QLineEdit()

        self.find_btn = QPushButton()
        self.find_btn.setText('查询')
        self.update_btn = QPushButton()
        self.update_btn.setText('更改')
        self.delete_btn = QPushButton()
        self.delete_btn.setText('删除')
        self.add_btn = QPushButton()
        self.add_btn.setText('添加')

        self.status_bar = self.statusBar()
        self.widget = QWidget()

        self.h1_layout = QHBoxLayout()
        self.h2_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        self.h1_layout.addWidget(self.find_text)
        self.h1_layout.addWidget(self.find_btn)
        self.h2_layout.addWidget(self.update_btn)
        self.h2_layout.addWidget(self.delete_btn)
        self.h2_layout.addWidget(self.add_btn)
        self.v_layout.addLayout(self.h1_layout)
        self.v_layout.addLayout(self.h2_layout)
        self.v_layout.addWidget(self.show_view)

        self.widget.setLayout(self.v_layout)
        self.setCentralWidget(self.widget)

    def connect_init(self):
        '''
        信号与槽
        '''
        '''查询功能'''
        self.find_btn.clicked.connect(lambda: self.find_slot(self.find_text.text()))

        '''添加功能'''
        self.add_btn.clicked.connect(self.add_slot)
        self.addwidget.add_close_signal.connect(lambda: self.setEnabled(True))
        self.addwidget.add_start_signal.connect(self.add_start_slot)

        '''删除功能'''
        self.delete_btn.clicked.connect(self.delete_slot)
        self.deletewidget.delete_close_signal.connect(lambda: self.setEnabled(True))
        self.deletewidget.delete_start_signal.connect(self.delete_start_slot)

        '''更新功能'''
        self.update_btn.clicked.connect(self.update_slot)
        self.updatewidget.update_close_signal.connect(lambda: self.setEnabled(True))
        self.updatewidget.update_name_signal.connect(self.update_name_slot)
        self.updatewidget.update_show_signal.connect(self.update_show_slot)

    def sql_init(self):
        '''
        sqllite3初始化
        '''
        try:
            conn = sqlite3.connect('find.db')
            cursor = conn.cursor()
            cursor.execute('select * from design')
            cursor.close()
            conn.close()
        except:
            conn = sqlite3.connect('find.db')
            cursor = conn.cursor()
            cursor.execute(
                'create table design (id  integer PRIMARY KEY AUTOINCREMENT,name varchar(20),show varchar(100))')
            cursor.close()
            conn.close()

        '''查询槽'''

    def find_slot(self, content):
        try:
            result = self.find_func(content)
            self.status_bar.showMessage('查询成功')
            if result:
                self.show_view.append('<font size="5" color="red">共查询 {} 条数据</font>'.format(len(result)))
                for i in range(len(result)):
                    self.show_view.append('id:' + str(result[i][0]))
                    self.show_view.append('名称：' + result[i][1])
                    self.show_view.append('说明：' + result[i][2])
            else:
                self.show_view.append('<font size="5" color="red">共查询 {} 条数据</font>'.format(len(result)))
        except Exception as e:
            self.status_bar.showMessage('查询失败')
            print(e)

        '''添加槽'''

    def add_slot(self):
        self.addwidget.show()
        self.setEnabled(False)

    def add_start_slot(self, name, show):
        try:
            self.insert_func(name, show)
            self.status_bar.showMessage('添加成功')
        except Exception as e:
            self.status_bar.showMessage('添加失败')
        self.setEnabled(True)
        self.addwidget.name_text.clear()
        self.addwidget.show_text.clear()
        self.addwidget.close()

        '''删除槽'''

    def delete_slot(self):
        self.deletewidget.show()
        self.setEnabled(False)

    def delete_start_slot(self, id):
        try:
            self.delete_func(id)
            self.reset_func()
            self.status_bar.showMessage('删除成功')
        except  Exception as e:
            self.status_bar.showMessage('删除失败')
        self.setEnabled(True)
        self.deletewidget.close()

        '''更新槽'''

    def update_slot(self):
        self.updatewidget.show()
        self.setEnabled(False)

    def update_name_slot(self, id, name):
        try:
            self.update_name_func(id, name)
            self.status_bar.showMessage('更新名称成功')
        except Exception as e:
            self.status_bar.showMessage('更新名称失败')
        self.setEnabled(True)
        self.updatewidget.close()

    def update_show_slot(self, id, name):
        try:
            self.update_show_func(id, name)
            self.status_bar.showMessage('更新名称成功')
        except Exception as e:
            self.status_bar.showMessage('更新名称失败')
        self.setEnabled(True)
        self.updatewidget.close()

        '''数据库函数'''

    def insert_func(self, name, show):
        '''
        插入信息
        '''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('insert into design (id,name,show) values (null,"{}","{}")'.format(name, show))
        cursor.close()
        conn.commit()
        conn.close()

    def find_func(self, content):
        '''
        查询信息
        '''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('SELECT * FROM design WHERE name LIKE "%{}%"'.format(content))
        result1 = cursor.fetchall()
        cursor.close()
        conn.close()
        return result1

    def count_func(self):
        '''
        信息数目
        '''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('select * from design')
        result = cursor.fetchall()
        count = len(result)
        cursor.close()
        conn.close()
        return count

    def update_name_func(self, id, name):
        '''
        更新名称信息
        '''
        print(id, name)
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('update design set name = ? where id = ?', (name, id))
        cursor.close()
        conn.commit()
        conn.close()

    def update_show_func(self, id, name):
        '''
        更新说明信息
        '''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('update design set show = ? where id = ?', (name, id))
        cursor.close()
        conn.commit()
        conn.close()

    def delete_func(self, id):
        '''
        删除信息
        '''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('delete from design where id = ?', (id,))
        cursor.close()
        conn.commit()
        conn.close()

    def reset_func(self):
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute("DELETE FROM sqlite_sequence WHERE name = 'design'")
        cursor.close()
        conn.commit()
        conn.close()


class AddWidget(QWidget):
    '''
    添加功能界面
    '''
    add_start_signal = pyqtSignal(str, str)
    add_close_signal = pyqtSignal()

    def __init__(self):
        super(AddWidget, self).__init__()
        self.ui_init()
        self.connect_init()

    def ui_init(self):
        self.setWindowTitle('添加信息')
        self.setFixedSize(300, 200)

        self.name_text = QLineEdit()
        self.show_text = QTextEdit()
        self.start_btn = QPushButton()
        self.start_btn.setText('添加')
        self.cancel_btn = QPushButton()
        self.cancel_btn.setText('取消')
        self.name_label = QLabel()
        self.name_label.setText('名称：')
        self.show_label = QLabel()
        self.show_label.setText('说明：')
        self.h1_layout = QHBoxLayout()
        self.h2_layout = QHBoxLayout()
        self.h3_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        self.h1_layout.addWidget(self.name_label)
        self.h1_layout.addWidget(self.name_text)
        self.h2_layout.addWidget(self.show_label)
        self.h2_layout.addWidget(self.show_text)
        self.h3_layout.addWidget(self.start_btn)
        self.h3_layout.addWidget(self.cancel_btn)
        self.v_layout.addLayout(self.h1_layout)
        self.v_layout.addLayout(self.h2_layout)
        self.v_layout.addLayout(self.h3_layout)
        self.setLayout(self.v_layout)

    def connect_init(self):
        self.start_btn.clicked.connect(self.start_slot)
        self.cancel_btn.clicked.connect(self.close_slot)

    def start_slot(self):
        name = self.name_text.text()
        show = self.show_text.toPlainText()
        self.add_start_signal.emit(name, show)

    def close_slot(self):
        self.close()
        self.add_close_signal.emit()

    def closeEvent(self, event):
        self.add_close_signal.emit()


class DeleteWidget(QWidget):
    '''
    删除功能界面
    '''
    delete_cancel_signal = pyqtSignal()
    delete_start_signal = pyqtSignal(str)
    delete_close_signal = pyqtSignal()

    def __init__(self):
        super(DeleteWidget, self).__init__()
        self.ui_init()
        self.connect_init()

    def ui_init(self):
        self.setWindowTitle('删除信息')
        self.setFixedSize(300, 100)

        self.id_text = QLineEdit()
        self.start_btn = QPushButton()
        self.start_btn.setText('删除')
        self.cancel_btn = QPushButton()
        self.cancel_btn.setText('取消')
        self.id_label = QLabel()
        self.id_label.setText('要删除的id：')
        self.h1_layout = QHBoxLayout()
        self.h2_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        self.h1_layout.addWidget(self.id_label)
        self.h1_layout.addWidget(self.id_text)
        self.h2_layout.addWidget(self.start_btn)
        self.h2_layout.addWidget(self.cancel_btn)
        self.v_layout.addLayout(self.h1_layout)
        self.v_layout.addLayout(self.h2_layout)
        self.setLayout(self.v_layout)

    def connect_init(self):
        self.start_btn.clicked.connect(self.start_slot)
        self.cancel_btn.clicked.connect(self.close_slot)

    def start_slot(self):
        id = self.id_text.text()
        self.delete_start_signal.emit(id)

    def close_slot(self):
        self.close()
        self.delete_close_signal.emit()

    def closeEvent(self, event):
        self.delete_close_signal.emit()


class UpdateWidget(QWidget):
    '''
    更新功能界面
    '''
    update_name_signal = pyqtSignal(int, str)
    update_show_signal = pyqtSignal(int, str)
    update_close_signal = pyqtSignal()

    def __init__(self):
        super(UpdateWidget, self).__init__()
        self.ui_init()
        self.connect_init()

    def ui_init(self):
        self.setWindowTitle('修改信息')
        self.setFixedSize(300, 150)

        self.content_text = QLineEdit()
        self.id_text = QLineEdit()
        self.name_btn = QPushButton()
        self.name_btn.setText('修改名称')
        self.show_btn = QPushButton()
        self.show_btn.setText('修改说明')
        self.cancel_btn = QPushButton()
        self.cancel_btn.setText('取消')
        self.content_label = QLabel()
        self.content_label.setText('修改内容：')
        self.id_label = QLabel()
        self.id_label.setText('选择id为:')
        self.h1_layout = QHBoxLayout()
        self.h2_layout = QHBoxLayout()
        self.h3_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        self.h1_layout.addWidget(self.id_label)
        self.h1_layout.addWidget(self.id_text)
        self.h2_layout.addWidget(self.content_label)
        self.h2_layout.addWidget(self.content_text)
        self.h3_layout.addWidget(self.name_btn)
        self.h3_layout.addWidget(self.show_btn)
        self.h3_layout.addWidget(self.cancel_btn)
        self.v_layout.addLayout(self.h1_layout)
        self.v_layout.addLayout(self.h2_layout)
        self.v_layout.addLayout(self.h3_layout)
        self.setLayout(self.v_layout)

    def connect_init(self):
        self.name_btn.clicked.connect(self.update_name_slot)
        self.show_btn.clicked.connect(self.update_show_slot)
        self.cancel_btn.clicked.connect(self.close_slot)

    def update_name_slot(self):
        id = self.id_text.text()
        id = int(id)
        content = self.content_text.text()
        self.update_name_signal.emit(id, content)

    def update_show_slot(self):
        id = self.id_text.text()
        id = int(id)
        content = self.content_text.text()
        self.update_show_signal.emit(id, content)

    def close_slot(self):
        self.close()
        self.update_close_signal.emit()

    def closeEvent(self, event):
        self.update_close_signal.emit()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    dispaly = MainWidget()
    dispaly.show()
    sys.exit(app.exec_())